//
//  TPBCallViewController.m
//  EncryptionTPB
//
//  Created by 吕剑锋 on 15/9/15.
//  Copyright © 2015年 coderjianfeng. All rights reserved.
//

#import "TPBCallViewController.h"
#import "UINavigationBar+Awesome.h"
#import "JFUtil.h"
#import "UIImage+ImageEffects.h"
#import "CallViewModel.h"

@interface TPBCallViewController ()<CallViewModelDelegate>{
    NSString *tempPhoneNumber;
    NSString *tempPhone;
}
@property (weak, nonatomic) UILabel *numberLabel;
@property (weak, nonatomic) UIButton *operatorButton;
@property (assign, nonatomic) int tapCount;
@end

@implementation TPBCallViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setCustomBackgroundColor:[UIColor clearColor]];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
}

- (void)setUI{
//    UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
//    backgroundImage.image = [UIImage imageNamed:@"bg.jpeg"];
//    [self.view addSubview:backgroundImage];
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];
    //配置界面
    UILabel *numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, SCREEN_WIDTH, 40)];
    numberLabel.textColor = [UIColor customGrayColor];
    numberLabel.textAlignment = NSTextAlignmentCenter;
    numberLabel.font = [UIFont systemFontOfSize:30];
    //numberLabel.text = @"13813828035";
    [topView addSubview:numberLabel];
    self.numberLabel = numberLabel;
    
    UIButton * operatorButton= [[UIButton alloc] initWithFrame:CGRectMake(0, 110, SCREEN_WIDTH, 40)];
    [operatorButton setTitleColor:[UIColor customGrayColor] forState:UIControlStateNormal];
    operatorButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    operatorButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [operatorButton setTitle:@"请输入号码" forState:UIControlStateNormal];
    //[operatorButton addTarget:self action:@selector(changeSetting) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:operatorButton];
    self.operatorButton = operatorButton;
    
    UIView *centerView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, SCREEN_WIDTH, SCREEN_HEIGHT-200-49)];
    centerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:centerView];
    
    CGFloat buttonW = (SCREEN_WIDTH-2)/3;
    CGFloat buttonH = (SCREEN_HEIGHT-200-49-2)/5;
    CGFloat Y;
    NSArray *array = [NSArray arrayWithObjects: [NSArray arrayWithObjects:@"1",@"2",@"3", nil],
                                                [NSArray arrayWithObjects:@"4",@"5",@"6", nil],
                                                [NSArray arrayWithObjects:@"7",@"8",@"9", nil],
                                                [NSArray arrayWithObjects:@"回退",@"0",@"粘贴", nil], nil];
    for (int i=0; i<5; i++) {
        Y = i*(buttonH+1);
        CGFloat X;
        if (i<4) {
            for (int j=0; j<3; j++) {
                X = j*(buttonW+1);
                UIButton *numberButton = [[UIButton alloc] init];
                numberButton.frame = CGRectMake(X, Y, buttonW, buttonH);
                NSString *value = [NSString stringWithFormat:@"%@",array[i][j]];
                [numberButton setTitle:value forState:UIControlStateNormal];
                [numberButton setTitleColor:[UIColor customGrayColor] forState:UIControlStateNormal];
                [numberButton setTitleColor:[UIColor groupTableViewBackgroundColor] forState:UIControlStateHighlighted];
                [numberButton setBackgroundImage:[JFUtil imageWithColor:[UIColor whiteColor] size:CGSizeMake(buttonW, buttonH)] forState:UIControlStateHighlighted];
                if ([value isEqualToString:@"回退"] || [value isEqualToString:@"粘贴"]) {
                    numberButton.titleLabel.font = [UIFont systemFontOfSize:21];
                }else{
                    numberButton.titleLabel.font = [UIFont systemFontOfSize:32];
                }
                numberButton.backgroundColor = [UIColor customControllerBackgroundColor];
                [numberButton addTarget:self action:@selector(clickNumberAction:) forControlEvents:UIControlEventTouchUpInside];
                [centerView addSubview:numberButton];
                
            }
        }else{
            UIView *callBackView = [[UIView alloc] initWithFrame:CGRectMake(0, Y, SCREEN_WIDTH, buttonH)];
            callBackView.backgroundColor = [UIColor whiteColor];
            [centerView addSubview:callBackView];
            
            UIButton *call = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/4, buttonH/2-(buttonH-20)/2, SCREEN_WIDTH/2, buttonH-20)];
            call.backgroundColor = [UIColor customGreenColor];
            call.layer.cornerRadius =(buttonH-20)/2;
            call.titleLabel.font = [UIFont systemFontOfSize:20];
            [call setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [call setTitle:@"呼叫" forState:UIControlStateNormal];
            [callBackView addSubview:call];
        }
    }
}

- (void)checkAutoGetBelongingPlace{
    self.tapCount = (int)self.numberLabel.text.length;
    if (self.tapCount >= 11) {
        if ([tempPhoneNumber isEqualToString:self.numberLabel.text]) {
            [self.operatorButton setTitle:tempPhone forState:UIControlStateNormal];
            return;
        }
        CallViewModel *model = [CallViewModel new];
        [model getBelongingPlaceWithNumber:self.numberLabel.text delegate:self];
        tempPhoneNumber = self.numberLabel.text;
    }else{
        [self.operatorButton setTitle:@"" forState:UIControlStateNormal];
    }
}

- (void)clickNumberAction:(UIButton*)sender{
    NSString *clickValue = sender.titleLabel.text;
    if ([clickValue isEqualToString:@"回退"]) {
        self.numberLabel.text = [JFUtil replaceBlankSpace:self.numberLabel.text];
        if (self.numberLabel.text.length>0) {
            NSMutableString *value = [NSMutableString stringWithString:self.numberLabel.text];
            [value deleteCharactersInRange:NSMakeRange(value.length-1, 1)];
            self.numberLabel.text = value;
        }
        [self checkAutoGetBelongingPlace];
    }else if([clickValue isEqualToString:@"粘贴"]){
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        NSString *number = [JFUtil replacePhoneNumber:pasteboard.string];
        BOOL isNumber = [JFUtil validateNumber:number];
        if (!isNumber) {
            [JFUtil showMBAlertLabel:self.navigationController.view text:@"没有号码可以粘贴，请输入" afterDelay:2.0];
            return;
        }
        self.numberLabel.text = number;
    }else{
        if (self.numberLabel.text.length<=0) {
            self.numberLabel.text = clickValue;
        }else{
            self.numberLabel.text = [self.numberLabel.text stringByAppendingString:clickValue];
        }
        [self checkAutoGetBelongingPlace];
    }
}

- (void)getBelongingPlaceModel:(Phone *)phone errCode:(int)code{
    if (!phone || code!=0) {
        DDDLog(@"归属地获取失败");
        [self.operatorButton setTitle:@"仅支持手机号码" forState:UIControlStateNormal];
        return;
    }
     NSString *title = [NSString stringWithFormat:@"%@%@%@",phone.province,phone.city,phone.operatorString];
    if ([phone.province isEqualToString:phone.city]) {
         title = [NSString stringWithFormat:@"%@%@",phone.city,phone.operatorString];
    }
   
    [self.operatorButton setTitle:title forState:UIControlStateNormal];
    tempPhone = title;
}
@end
